package cn.itsource.service.impl;

import cn.itsource.domain.Teacher;
import cn.itsource.mapper.TeacherMapper;
import cn.itsource.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class TeacherServiceImpl implements ITeacherService {

    @Autowired
    private TeacherMapper teacherMapper;
    @Override
    @Transactional
    public void save(Teacher teacher) {
        //如果id为空就是新增
        if (teacher.getId() == null) {
            teacherMapper.save(teacher);
        }else{
            teacherMapper.update(teacher);
        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        teacherMapper.delete(id);
    }

    @Override
    public Teacher get(Long id) {
        return teacherMapper.get(id);
    }

    @Override
    public List<Teacher> getAll() {
        return teacherMapper.getAll();
    }

    @Override
    public void batchInsert(List<Teacher> teachers) {
        //如果列表为0，就直接结束程序
        if(teachers.size() == 0){
            return;
        }
        //批量插入老师数据
        teacherMapper.batchInsert(teachers);
    }
}
